﻿namespace DotNetDream.Common
{
    public static class Extension
    {
        /// <summary>
        /// Friendly shorten a string
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Shorten(this string str, int length)
        {
            if (str.Length > length)
            {
                string result = string.Empty;
                string[] parts = str.Split(' ');
                int partCount = 0;
                if (parts.Length > 1)
                {
                    while (result.Length <= length)
                    {
                        result += parts[partCount] + ";";
                        partCount++;
                    }
                    str = result.TrimEnd(';').Replace(';', ' ') + "...";
                }
            }
            return str;
        }
    }
}
